
/**
 * 获取一个DIV的绝对坐标的功能函数,即使是非绝对定位,一样能获取到
 * 
 * @param dom
 * @returns 
 */
function getElCoordinate(dom) {
  var t = dom.offsetTop;
  var l = dom.offsetLeft;
  dom=dom.offsetParent;
  while (dom) {
    t += dom.offsetTop;
    l += dom.offsetLeft;
	dom=dom.offsetParent;
  }; 
  
  return {
    top: t,
    left: l
  };
}

/**
 * 兼容各种浏览器的,获取鼠标真实位置
 * 
 * @param ev
 * @returns
 */
function mousePosition(ev){
	if(!ev) ev=window.event;
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.documentElement.scrollTop  - document.body.clientTop
    };
}

/**
 * 给DATE类添加一个格式化输出字串的方法
 */
Date.prototype.format = function(format){   
   var o = {   
      "M+" : this.getMonth()+1, //month  
      "d+" : this.getDate(),    //day  
      "h+" : this.getHours(),   //hour  
      "m+" : this.getMinutes(), //minute  
      "s+" : this.getSeconds(), //second  
	  //quarter  
      "q+" : Math.floor((this.getMonth()+3)/3), 
      "S" : this.getMilliseconds() //millisecond  
   }   
   if(/(y+)/.test(format)){
	   format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));   
   } 
    for(var k in o){
    	if(new RegExp("("+ k +")").test(format)){
    		format = format.replace(RegExp.$1,RegExp.$1.length===1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));   
    	}
    }
    return format;   
}

/**
 * JS根据格式字符串分析日期（MM与自动匹配两位的09和一位的9）
 * 
 * @param dateString
 * @param formatString
 * @returns {Date}
 */
function getDateFromFormat(dateString,formatString){
	var regDate = /\d+/g;
	var regFormat = /[YyMmdHhSs]+/g;
	var dateMatches = dateString.match(regDate);
	var formatmatches = formatString.match(regFormat);
	var date = new Date();
	for(var i=0;i<dateMatches.length;i++){
		switch(formatmatches[i].substring(0,1)){
			case 'Y':
			case 'y':
				date.setFullYear(parseInt(dateMatches[i]));
				break;
			case 'M':
				date.setMonth(parseInt(dateMatches[i])-1);
				break;
			case 'd':
				date.setDate(parseInt(dateMatches[i]));
				break;
			case 'H':
			case 'h':
				date.setHours(parseInt(dateMatches[i]));
				break;
			case 'm':
				date.setMinutes(parseInt(dateMatches[i]));
				break;
			case 's':
				date.setSeconds(parseInt(dateMatches[i]));
				break;
			default:
				break;
		}
	}
	return date;
}

/**
 * 货币分析成浮点数
 * 
 * @param currentString
 * @returns
 */
function parseCurrency(currentString){
	var regParser = /[\d\.]+/g;
	var matches = currentString.match(regParser);
	var result = '';
	var dot = false;
	for(var i=0;i<matches.length;i++){
		var temp = matches[i];
		if(temp === '.' && dot){
			continue;
		}
		result += temp;
	}
	return parseFloat(result);
}

/**
 * 将#XXXXXX颜色格式转换为RGB格式，并附加上透明度
 * 
 * @param hex
 * @param opacity
 * @returns
 */
function brgba(hex, opacity) {
    if( ! /#?\d+/g.test(hex) ){
    	return hex; //如果是“red”格式的颜色值，则不转换。//正则错误，参考后面的PS内容
    } 
    var h = hex.charAt(0) === "#" ? hex.substring(1) : hex,
        r = parseInt(h.substring(0,2),16),
        g = parseInt(h.substring(2,4),16),
        b = parseInt(h.substring(4,6),16),
        a = opacity;
    return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}


/**
 * 提示信息弹出框
 * 
 * @param content
 * @param callbackFunction
 */
function showAlert(content,callbackFunction){
	
	new $.layer({
		content:content,
		title:'提示信息',
		type:'alert',
		width:386,
		height:200,
		onConfirmButton:callbackFunction
	}).show();
}
/**
 * 异步初始化单个select框
 * 
 * @param url
 * @param selectObject
 * @param textKey
 * @param valueKey
 */
function initSelectData(url,selectObject,textKey,valueKey,nullOptionText){
	
	var selectObjectArray = [];
	selectObjectArray.push(selectObject);
	
	initSelectDataBatch(url, selectObjectArray, textKey, valueKey,nullOptionText);
	
}

/**
 * 异步初始化多个select框(数据相同)
 * 
 * @param url
 * @param selectObjectArray
 * @param textKey
 * @param valueKey
 */
function initSelectDataBatch(url,selectObjectArray,textKey,valueKey,nullOptionText){
	
	$.ajax({  
        url:url,  
        type:'post',  
        dataType:'json',  
        success:function(data,textStatus,jqXHR){  
        	
        	$(selectObjectArray).each(function(index,entry){
        		if(!nullOptionText){
        			nullOptionText = '请选择';
        		}
    			$(entry).append('<option value="">'+nullOptionText+'</option>');
            	
            	$(data.data).each(function(number,element){
            		var optionObj = $('<option></option>').append(element[textKey]).attr('value',element[valueKey]);
            		$(entry).append(optionObj);
            	});
        	});
        	
        },  
        error:function(XMLHttpRequest,textStatus,errorThrown){  
        }                     
    });
}

/**
 * 组织机构选择层 
 * 目前只能单选
 * 
 * @param basePath
 * @param domainId
 * @param confrimFunction
 */
function organizationSelectTree(basePath,domainId,confrimFunction){
	var url = basePath + '/biz/ou/org/showOrganizationTreeSelect.do?domainId='+domainId;
	
	var layer = new $.layer({
		type:'iframe',
		title: '组织机构选择',
		width:340,
		height:300,
		content:url,
		showMatte:false,
		buttonArray:[
		   {
			   name: '取消',
			   callback:function(){
				   layer.close();
			   }
		   },
		   
		   {
			   name: '确定',
			   callback:function(){
				   var iframeObj = layer.getIFrame();
				   var pageData = iframeObj[0].contentWindow.onConfrim();
				   confrimFunction(pageData,layer);
			   }
		   }
		]
	});
	
	layer.show();
	
}

/**
 * 群组选择层 
 * 目前只能单选
 * 
 * @param basePath
 * @param domainId
 * @param confrimFunction
 */
function groupSelectTree(basePath,domainId,confrimFunction){
	var url = basePath + '/biz/ou/group/showGroupTree.do?domainId='+domainId;
	
	var layer = new $.layer({
		type:'iframe',
		title: '群组选择',
		width:340,
		height:300,
		content:url,
		showMatte:false,
		buttonArray:[
		   {
			   name: '取消',
			   callback:function(){
				   layer.close();
			   }
		   },
		   
		   {
			   name: '确定',
			   callback:function(){
				   var iframeObj = layer.getIFrame();
				   var pageData = iframeObj[0].contentWindow.onConfrim();
				   confrimFunction(pageData,layer);
			   }
		   }
		]
	});
	
	layer.show();
	
}
	

		

